// pages/mine/bankCard/bindCard.js

const App = getApp()

let { AJAX, timeTranfer,imgUrl } = require("../../../utils/util.js")


Page({ 
   /**
    * 页面的初始数据
    */
   data: {
      navH:0,
      bankCard:'',
      isBindCard:false,
      bankInfo:'',
      bankCard:'', // 转码,
      codeShow:true,
      YCard:'',
      yesSrc:"http://static.xzs51.com/image/off.png",
      noSrc:"http://static.xzs51.com/image/on.png",
     imgUrl: imgUrl
   }, 

   /**
    * 生命周期函数--监听页面加载
    */
   onLoad: function (options) {
      let vm = this
      this.setData({
         navH: App.globalData.navHeight
      })
      if (!wx.getStorageSync("ACCESSTOKEN")) {
         App.userInfoReadyCallback = res => {
            wx.setStorageSync('ACCESSTOKEN', res);
            vm.getBank()
         }
         return
      }
      vm.getBank()
   },

   /**
    * 生命周期函数--监听页面初次渲染完成
    */
   onReady: function () {

   },

   /**
    * 生命周期函数--监听页面显示
    */
   onShow: function () {

   },

   /**
    * 生命周期函数--监听页面隐藏
    */
   onHide: function () {

   },

   /**
    * 生命周期函数--监听页面卸载
    */
   onUnload: function () {

   },

   /**
    * 页面相关事件处理函数--监听用户下拉动作
    */
   onPullDownRefresh: function () {

   },

   /**
    * 页面上拉触底事件的处理函数
    */
   onReachBottom: function () {

   },

   /**
    * 用户点击右上角分享
    */
   onShareAppMessage: function () {

   },
   // 点击修改
   reviseCard(){
      wx.navigateTo({
         url: '/pages/mine/addCard/addCard',
      })
   },
   /**bank/get-bank
    * 获取用户绑定的银行卡
    */
   getBank(){
      AJAX("bank/get-bank").then(res => {
         if(res.data!=''){
            let bankCard = res.data.cardno.replace(/\s/g, '').replace(/(\d{4})\d+(\d{4})$/, "**** **** **** $2")

            let YCard = res.data.cardno.replace(/\s/g, '').replace(/[^\d]/g, '').replace(/(\d{4})(?=\d)/g, '$1 ')

            if (res.have_data == true) {
               this.setData({
                  isBindCard: true,
                  bankInfo: res.data,
                  bankCard: bankCard,
                  YCard: YCard
               })
            }
         }
       
      }) 
   },
   // 显示转码
   zhuanCode(){
      this.setData({
         codeShow:!this.data.codeShow
      })
   },
   //添加银行卡
   addCard() {      
      wx.navigateTo({
         url: '/pages/mine/addCard/addCard',
      })
   },
   navBack() {
      wx.switchTab({
         url: '/pages/mine/mine',
      })
   },
   BankCardNoInput(e) {
      var card = e.detail.value;
      card = card.replace(/\s/g, '').replace(/[^\d]/g, '').replace(/(\d{4})(?=\d)/g, '$1 ');
      this.setData({
         bankCard: card,
      })
   }
})